﻿//------------------------------------------------------------------------------
//  Namespace: FruitVentDesignDemo.ViewModel
//  
//  Function： N/A
//  Name： TablePageViewModel
//  
//  Ver       Time                     Author
//  0.10      2021/6/28 10:36:50      FruitVent
//
//  此代码版权归作者本人FruitVent所有
//  源代码使用协议遵循本仓库的开源协议及附加协议，若本仓库没有设置，则按MIT开源协议授权
//  CSDN博客：https://blog.csdn.net/weixin_39552347
//  源代码仓库：https://gitee.com/fruitvent
//  感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using FruitVentDesign.Models;
using FruitVentDesignDemo.Enums;
using FruitVentDesignDemo.Model;
using FruitVentDesignDemo.Util;
using FruitVentMVVM;
using FruitVentMVVM.Command;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;

namespace FruitVentDesignDemo.ViewModel
{
    public class TableViewModel : ViewModelBase
    {
        public TableViewModel()
        {
            Debug.WriteLine("TablePageViewModel");

            IdWorker idWorker = new IdWorker(0, 0);
            List<Student> students = new List<Student>
            {
                new Student(idWorker.NextId().ToString(), "小明", 7, "一年级", SexEnum.Male, new TextInfo(){ Value = "1", Label = "一" }),
                new Student(idWorker.NextId().ToString(), "小陈", 6, "一年级", SexEnum.Male, new TextInfo(){ Value = "1", Label = "一" }),
                new Student(idWorker.NextId().ToString(), "小郑", 7, "一年级", SexEnum.Male, new TextInfo(){ Value = "1", Label = "一" }),
                new Student(idWorker.NextId().ToString(), "小芳", 7, "一年级", SexEnum.Female, new TextInfo(){ Value = "1", Label = "一" }),
                new Student(idWorker.NextId().ToString(), "小虎", 8, "二年级", SexEnum.Male, new TextInfo(){ Value = "1", Label = "一" }),
                new Student(idWorker.NextId().ToString(), "小红", 8, "二年级", SexEnum.Female, new TextInfo(){ Value = "1", Label = "一" }),
                new Student(idWorker.NextId().ToString(), "小王", 9, "三年级", SexEnum.Male, new TextInfo(){ Value = "2", Label = "二" }),
                new Student(idWorker.NextId().ToString(), "小蓝", 9, "三年级", SexEnum.Male, new TextInfo(){ Value = "2", Label = "二" }),
                new Student(idWorker.NextId().ToString(), "小华", 10, "四年级", SexEnum.Male, new TextInfo(){ Value = "2", Label = "二" }),
                new Student(idWorker.NextId().ToString(), "小小", 11, "五年级", SexEnum.Female, new TextInfo(){ Value = "2", Label = "二" }),
                new Student(idWorker.NextId().ToString(), "小张", 12, "六年级", SexEnum.Male, new TextInfo(){ Value = "2", Label = "二" })
            };
            Students = new ObservableCollection<Student>(students);

            List<FilterItem> textFilterItems = new List<FilterItem>
            {
                new FilterItem() { Key = "1",  DisplayText = "一" },
                new FilterItem() { Key = "2", DisplayText = "二" },
            };

            TextFilterItems = textFilterItems;
        }

        /// <summary>
        /// student list
        /// </summary>
        private ObservableCollection<Student> _students;
        public ObservableCollection<Student> Students
        {
            get
            {
                return _students;
            }
            set
            {
                if (_students != value)
                {
                    _students = value;
                    OnPropertyChanged(nameof(Students));
                }
            }
        }

        /// <summary>
        /// grade filter items
        /// </summary>
        public IEnumerable<FilterItem> GradeFilterItems
        {
            get
            {
                List<FilterItem> items = new List<FilterItem>
                {
                    new FilterItem() { DisplayText = "一年级" },
                    new FilterItem() { DisplayText = "二年级" },
                    new FilterItem() { DisplayText = "三年级" },
                    new FilterItem() { DisplayText = "四年级" },
                    new FilterItem() { DisplayText = "五年级" },
                    new FilterItem() { DisplayText = "六年级" },
                    new FilterItem() { DisplayText = "七年级" },
                    new FilterItem() { DisplayText = "八年级" },
                    new FilterItem() { DisplayText = "九年级" }
                };
                return items;
            }
        }

        /// <summary>
        /// sex filter items
        /// </summary>
        public IEnumerable<FilterItem> SexFilterItems
        {
            get
            {
                List<FilterItem> items = new List<FilterItem>
                {
                    new FilterItem() { Key = SexEnum.Male,  DisplayText = "男" },
                    new FilterItem() { Key = SexEnum.Female, DisplayText = "女" },
                };
                return items;
            }
        }

        /// <summary>
        /// sex filter items
        /// </summary>
        private IEnumerable<FilterItem> _textFilterItems;
        public IEnumerable<FilterItem> TextFilterItems
        {
            get
            {
                return _textFilterItems;
            }
            set
            {
                if (_textFilterItems != value)
                {
                    _textFilterItems = value;
                    OnPropertyChanged(nameof(TextFilterItems));
                }
            }
        }

        /// <summary>
        /// get filter sources command
        /// </summary>
        private RelayCommand<List<object>> _getFilterSources;
        public RelayCommand<List<object>> GetFilterSources
        {
            get
            {
                if(_getFilterSources == null)
                {
                    _getFilterSources = new RelayCommand<List<object>>(OnGetFilterSources);
                }
                return _getFilterSources;
            }
        }

        /// <summary>
        /// get filter sources
        /// </summary>
        /// <param name="obj"></param>
        private void OnGetFilterSources(List<object> obj)
        {
            if(obj != null)
            {
                foreach(var item in obj)
                {
                    Student student = (Student)item;
                }
            }
        }

        public int[] PageSizeOptions { get; set; } = new int[] { 3, 6, 9, 12 };

        /// <summary>
        /// 添加Command
        /// </summary>
        private RelayCommand _addCommand;
        public RelayCommand AddCommand
        {
            get
            {
                _addCommand = new RelayCommand(OnAdd);
                return _addCommand;
            }
        }

        private void OnAdd()
        {
            IdWorker idWorker = new IdWorker(0, 0);
            Student student = new Student(idWorker.NextId().ToString(), "小李", 7, "一年级", SexEnum.Male, new TextInfo() { Value = "1", Label = "一" });

            List<Student> students = Students.ToList();

            students.Add(student);

            Students = new ObservableCollection<Student>(students);
        }

        /// <summary>
        /// 修改
        /// </summary>
        private RelayCommand<Student> _updateCommand;
        public RelayCommand<Student> UpdateCommand
        {
            get
            {
                _updateCommand = new RelayCommand<Student>(OnUpdate);
                return _updateCommand;
            }
        }

        private void OnUpdate(Student student)
        {
            //List<FilterItem> textFilterItems = new List<FilterItem>
            //{
            //    new FilterItem() { Key = "1",  DisplayText = "一" },
            //    new FilterItem() { Key = "2", DisplayText = "二" },
            //};

            //TextFilterItems = textFilterItems;

            List<Student> students = Students.ToList();

            foreach (var item in students)
            {
                if (item.Id == student.Id)
                {
                    item.Text = new TextInfo() { Value = "2", Label = "二" };
                }
            }

            Students = new ObservableCollection<Student>(students);
        }

        /// <summary>
        /// 删除
        /// </summary>
        private RelayCommand<Student> _deleteCommand;
        public RelayCommand<Student> DeleteCommand
        {
            get
            {
                _deleteCommand = new RelayCommand<Student>(OnDelete);
                return _deleteCommand;
            }
        }

        private void OnDelete(Student student)
        {
            Students.Remove(student);
        }
    }
}
